package com.mvp.shq.washer.ui.orderDetail;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.OrderInfo;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.DateUtils;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.T;
import com.mvp.shq.washer.view.widget.CustomDialog;
import com.mvp.shq.washer.view.widget.EditDialog;
import com.zhy.autolayout.AutoRelativeLayout;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderDetailActivity extends BaseActivity<OrderDetailPresenter, OrderDetailModel> implements OrderDetailConstract.View {

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tv_order_no)
    TextView tvOrderNo;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.tv_order_time)
    TextView tvOrderTime;
    @BindView(R.id.img_washer)
    ImageView imgWasher;
    @BindView(R.id.tv_washer_name)
    TextView tvWasherName;
    @BindView(R.id.tv_washer_no)
    TextView tvWasherNo;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.img_icon)
    ImageView imgIcon;
    @BindView(R.id.tv_pattern)
    TextView tvPattern;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.arl_bg)
    AutoRelativeLayout arlBg;
    @BindView(R.id.tv_pay_money)
    TextView tvPayMoney;
    @BindView(R.id.tv_money_unit)
    TextView tvMoneyUnit;
    @BindView(R.id.btn_error_order)
    Button btnErrorOrder;
    private String orderNo;
    private int errotFlag;
    private String washerNo;
    private String orderStatus;
    private EditDialog dialog;
    private CustomDialog customDialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_order_detail;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.white), 0);
        title.setText("订单详情");
        imageUtil.loadResImage(R.mipmap.order_pattern_select,imgWasher);
        orderNo = this.getIntent().getExtras().getString("orderNo");
        showLoadingDialog(true);
        presenter.getOrders(orderNo);
    }


    @OnClick({R.id.back,R.id.btn_error_order})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.btn_error_order:
                if (DataDictionary.WASHER_ORDER_STATUS_FINISH.equals(orderStatus)){
                    //订单完成,再来一单
                    JumpUtil.toOrderConfirm(context,washerNo);
                }else if (DataDictionary.WASHER_ORDER_STATUS_ING.equals(orderStatus)){
                    //订单进行中,提交异常
                    if (customDialog == null){
                        CustomDialog.Builder builder = new CustomDialog.Builder(context);
                        customDialog = builder.setMessage("是否强制结束洗衣？\n")
                                .setNegativeButton("确认", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        customDialog.dismiss();
                                        showLoadingDialog(true);
                                        presenter.setErrorOrder(orderNo);
                                    }
                                })
                                .setPositiveButton("取消", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        customDialog.dismiss();
                                    }
                                }).createTwoButtonDialog();
                    }
                    customDialog.show();
                }else if (DataDictionary.WASHER_ORDER_STATUS_ERROR.equals(orderStatus)){
                    //订单异常,申请退款
                    if (dialog == null){
                        EditDialog.Builder builder = new EditDialog.Builder(context);
                        dialog = builder.setMessage("请输入退款原因")
                                .setEditListener(new EditDialog.editListener() {
                                    @Override
                                    public void save(String content) {
                                        showLoadingDialog(true);
                                        presenter.refund(orderNo,content);
                                        dialog.dismiss();
                                    }

                                    @Override
                                    public void cancel() {
                                        dialog.dismiss();
                                    }
                                }).createTwoButtonDialog();
                    }

                    dialog.show();
                }
                break;
            default:
                return;
        }
    }

    @Override
    public void getOrderS(OrderInfo order) {
        if (order == null){
            T.show("未知错误");
            return;
        }
        btnErrorOrder.setVisibility(View.VISIBLE);
        errotFlag = order.getErrorFlag();
        orderStatus = order.getStatus();
        washerNo = order.getWasherNo();
        tvOrderNo.setText("订单号:"+order.getOrderNo());
        String statusStr = "";
        if (DataDictionary.WASHER_ORDER_PAY_STATUS_PAY.equals(order.getPayStatus())){
            if (DataDictionary.WASHER_ORDER_STATUS_ERROR.equals(order.getStatus())){
                statusStr = "异常";
                btnErrorOrder.setText("申请退款");
            }else if (DataDictionary.WASHER_ORDER_STATUS_NOT_PAY.equals(order.getStatus())){
                statusStr = "未支付";
                btnErrorOrder.setVisibility(View.GONE);
            }else if (DataDictionary.WASHER_ORDER_STATUS_ING.equals(order.getStatus())){
                statusStr = "进行中";
                btnErrorOrder.setText("强制结束");
            }else if (DataDictionary.WASHER_ORDER_STATUS_FINISH.equals(order.getStatus())){
                statusStr = "已完成";
                btnErrorOrder.setText("再来一单");
            }
        }else if (DataDictionary.WASHER_ORDER_PAY_STATUS_REFUND_ING.equals(order.getPayStatus())){
            statusStr = "退款中";
            btnErrorOrder.setVisibility(View.GONE);
        }else if (DataDictionary.WASHER_ORDER_PAY_STATUS_REFUND.equals(order.getPayStatus())){
            statusStr = "已退款";
            btnErrorOrder.setVisibility(View.GONE);
        }
        tvStatus.setText(statusStr);
        Date d = DateUtils.StrToDate(order.getCreateDate());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd HH:mm");
        //格式化为日+ 9期/时间字符串
        String cc=sdf.format(d);
        tvOrderTime.setText(cc);
        tvWasherName.setText(order.getWasherBrand()+" "+order.getWasherSpecDes());
        tvWasherNo.setText("NO."+order.getWasherNo());
        tvAddress.setText(order.getWasherAddress());
        tvPattern.setText(order.getPatternName());
        tvTime.setText(order.getPatternTime()+"min");
        if ("3".equals(order.getPayType())){
            tvMoneyUnit.setVisibility(View.GONE);
            tvPayMoney.setText("优惠券");
        }else {
            tvMoneyUnit.setVisibility(View.VISIBLE);
            tvPayMoney.setText(order.getTotalMoney()+"");
        }
        tvMoney.setText("￥"+order.getPatternPrice());
        imageUtil.loadResImage(R.mipmap.washing_machine,imgWasher);
    }

    @Override
    public void getOrderF(String msg) {
        T.show(msg);
    }

    @Override
    public void setErrorOrderS() {
        T.show("设置成功");
        RxBus.get().send(EventCode.REFRESH_ORDER);
        finish();
    }

    @Override
    public void refundS() {
        T.show("申请成功");
        finish();
    }
}
